package com.senarai.thedemit;


import javax.microedition.khronos.opengles.GL10;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.background.ColorBackground;
import org.anddev.andengine.entity.scene.menu.MenuScene;
import org.anddev.andengine.entity.scene.menu.MenuScene.IOnMenuItemClickListener;
import org.anddev.andengine.entity.scene.menu.item.IMenuItem;
import org.anddev.andengine.entity.scene.menu.item.SpriteMenuItem;
import org.anddev.andengine.entity.util.FPSLogger;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.ui.activity.BaseGameActivity;

import android.view.Display;

public class MainMenu extends BaseGameActivity implements IOnMenuItemClickListener{

	//Constant :
	protected static final int MENU_PROFILE = 0;
	protected static final int MENU_GHOSTPEDIA = MENU_PROFILE + 1;
	protected static final int MENU_ACHIEVEMENTS = MENU_GHOSTPEDIA + 1;
	protected static final int MENU_ABOUT = MENU_ACHIEVEMENTS + 1;
	protected static final int MENU_EXIT = MENU_ABOUT + 1;
	
	
	//Field :
	private int CAMERA_WIDTH;
	private int CAMERA_HEIGHT;
	protected Scene mMainScene;
	private Camera mCamera;
	
	//Menu : 
	protected MenuScene mMenuScene;
	private BitmapTextureAtlas mMenuTexture;
	protected TextureRegion mMenuProfileTextureRegion;
	protected TextureRegion mMenuGhostpediaTextureRegion;
	protected TextureRegion mMenuAchievementsTextureRegion;
	protected TextureRegion mMenuAboutTextureRegion;
	protected TextureRegion mMenuExitTextureRegion;
	
	public void onLoadComplete() {
		// TODO Auto-generated method stub
		
	}

	public Engine onLoadEngine() {
		final Display display = getWindowManager().getDefaultDisplay();
		CAMERA_WIDTH = display.getWidth();
		CAMERA_HEIGHT = display.getHeight();
		
		this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
		return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera));
	}

	public void onLoadResources() {
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
		
		this.mMenuTexture = new BitmapTextureAtlas(512, 512, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.mMenuProfileTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mMenuTexture, this, "profile.png", 0, 0);
		this.mMenuGhostpediaTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mMenuTexture, this, "ghostpedia.png", 0, 60);
		this.mMenuAchievementsTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mMenuTexture, this, "achievements.png", 0, 120);
		this.mMenuAboutTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mMenuTexture, this, "about.png", 0, 180);
		this.mMenuExitTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mMenuTexture, this, "exit.png", 0, 240);
		this.mEngine.getTextureManager().loadTexture(this.mMenuTexture);
		
	}

	public Scene onLoadScene() {
		this.mEngine.registerUpdateHandler(new FPSLogger());
		
		//Crate Main Scene : 
		this.mMainScene = new Scene();
		this.mMainScene.setBackground(new ColorBackground(0.0f, 0.0f, 0.0f, 0.0f));
		
		//Create Scene Menu :
		this.createMenuScene();
		
		return this.mMainScene;
	}
	
	
	//Methods :
	public void createMenuScene() {
		this.mMenuScene = new MenuScene(this.mCamera);		
		//Menu Profile
		final SpriteMenuItem profileMenuItem = new SpriteMenuItem(MENU_PROFILE, this.mMenuProfileTextureRegion);
		profileMenuItem.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		this.mMenuScene.addMenuItem(profileMenuItem);

		//Menu Ghostpedia
		final SpriteMenuItem ghostpediaMenuItem = new SpriteMenuItem(MENU_GHOSTPEDIA, this.mMenuGhostpediaTextureRegion);
		ghostpediaMenuItem.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		this.mMenuScene.addMenuItem(ghostpediaMenuItem);

		//Menu Achievements
		final SpriteMenuItem achievementsMenuItem = new SpriteMenuItem(MENU_ACHIEVEMENTS, this.mMenuAchievementsTextureRegion);
		achievementsMenuItem.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		this.mMenuScene.addMenuItem(achievementsMenuItem);
		
		//Menu About
		final SpriteMenuItem aboutMenuItem = new SpriteMenuItem(MENU_GHOSTPEDIA, this.mMenuAboutTextureRegion);
		aboutMenuItem.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		this.mMenuScene.addMenuItem(aboutMenuItem);
		
		//Menu Exit
		final SpriteMenuItem exitMenuItem = new SpriteMenuItem(MENU_EXIT, this.mMenuExitTextureRegion);
		exitMenuItem.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		this.mMenuScene.addMenuItem(exitMenuItem);
		
		this.mMenuScene.buildAnimations();

		this.mMenuScene.setBackgroundEnabled(false);

		this.mMenuScene.setOnMenuItemClickListener(this);
		this.mMainScene.setChildScene(this.mMenuScene);
	}

	public boolean onMenuItemClicked(MenuScene pMenuScene, IMenuItem pMenuItem,
			float pMenuItemLocalX, float pMenuItemLocalY) {
		switch(pMenuItem.getID()) {
			case MENU_PROFILE:
				return true;
			case MENU_GHOSTPEDIA:
				return true;
			case MENU_ACHIEVEMENTS:
				return true;
			case MENU_ABOUT:
				return true;
			case MENU_EXIT:
				this.finish();
				return true;
			default:
				return false;
		}
	}
}
